package com.itheima.travel.web;

import com.itheima.travel.enums.StatusEnum;
import com.itheima.travel.exception.ProjectException;
import com.itheima.travel.req.UserVo;
import com.itheima.travel.res.ResponseWrap;
import com.itheima.travel.service.UserService;
import com.itheima.travel.utils.EmptyUtil;
import com.itheima.travel.utils.ExceptionsUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Date;

/*
* 声明bean给spring管理，定义返回类型为json
* 定义映射路径
* 日志引入
* 解决跨域问题
* */
@RestController
@RequestMapping("user")
@Log4j2
@Api(tags = "用户操作")
@CrossOrigin
public class UserController {

    @Autowired
    UserService userService;

    @Autowired
    HttpSession session;

    //注册
    @PostMapping("register")
    @ApiOperation(value = "用户注册",notes = "用户注册")
    @ApiImplicitParam(name = "userVo",value = "注册信息",required = true,dataType = "UserVo")
    public ResponseWrap<Boolean> registerUser(UserVo userVo){
        try {
            Boolean flag = userService.registerUser(userVo);
            return ResponseWrap.<Boolean>builder()
                    .code(StatusEnum.SUCCEED.getCode())
                    .msg(StatusEnum.SUCCEED.getMsg())
                    .operationTime(new Date())
                    .data(flag)
                    .build();
        } catch (Exception e) {
            //打印错误信息
            log.error("用户注册失败：{}", ExceptionsUtil.getStackTraceAsString(e));
            //1.异常丢回前端;这种性能好些
            return ResponseWrap.<Boolean>builder()
                    .code(StatusEnum.FAIL.getCode())
                    .msg(StatusEnum.FAIL.getMsg())
                    .operationTime(new Date())
                    .data(false)
                    .build();
            //2.对于异常处理第二种方式,抛出异常
            /*throw new ProjectException(
                    StatusEnum.REGISTER_USER_FAIL.getCode(),
                    StatusEnum.REGISTER_USER_FAIL.getMsg()
            );*/
        }
    }

    //登录
    @PostMapping("login")
    @ApiOperation(value = "用户登录",notes = "用户登录")
    @ApiImplicitParam(name = "userVo",value = "登录信息",required = true,dataType = "UserVo")
    public ResponseWrap<UserVo> loginUser(@RequestBody UserVo userVo) throws ProjectException {
        try {
            UserVo userVoResult = userService.loginUser(userVo);
            return ResponseWrap.<UserVo>builder()
                    .code(StatusEnum.SUCCEED.getCode())
                    .msg(StatusEnum.SUCCEED.getMsg())
                    .operationTime(new Date())
                    .data(userVoResult)
                    .build();
        } catch (Exception e) {
            log.error("用户登录失败：{}",ExceptionsUtil.getStackTraceAsString(e));
            throw new ProjectException(
                    StatusEnum.LOGIN_USER_FAIL.getCode(),
                    StatusEnum.LOGIN_USER_FAIL.getMsg()
            );
        }
    }

    //退出
    @GetMapping("logout")
    @ApiOperation(value = "用户退出",notes = "用户退出")
    public ResponseWrap<Boolean> loginOutUser() throws ProjectException {
        try {
            userService.loginOutUser();
            return ResponseWrap.<Boolean>builder()
                    .code(StatusEnum.SUCCEED.getCode())
                    .msg(StatusEnum.SUCCEED.getMsg())
                    .operationTime(new Date())
                    .data(true)
                    .build();
        } catch (Exception e) {
            log.error("是否登录：{}", ExceptionsUtil.getStackTraceAsString(e));
            throw new ProjectException(StatusEnum.FAIL.getCode(),
                    StatusEnum.FAIL.getMsg());
        }
    }

    //是否登录
    @GetMapping("islogin")
    @ApiOperation(value = "是否登录",notes = "是否登录")
    public ResponseWrap<Boolean> isLogin() throws ProjectException {
        try {
            Boolean flag = userService.isLogin();
            return ResponseWrap.<Boolean>builder()
                    .code(StatusEnum.SUCCEED.getCode())
                    .msg(StatusEnum.SUCCEED.getMsg())
                    .operationTime(new Date())
                    .data(flag)
                    .build();
        } catch (Exception e) {
            log.error("是否登录：{}", ExceptionsUtil.getStackTraceAsString(e));
            throw new ProjectException(StatusEnum.ISLOGIN_FAIL.getCode(),
                    StatusEnum.ISLOGIN_FAIL.getMsg());
        }
    }

    //当前用户
    @GetMapping("currentUser")
    @ApiOperation(value = "当前用户",notes = "当前用户")
    public ResponseWrap<UserVo> currentUser() throws ProjectException {
        Object obj = session.getAttribute(session.getId());
        try {
            if (!EmptyUtil.isNullOrEmpty(obj)){
                return ResponseWrap.<UserVo>builder()
                        .code(StatusEnum.SUCCEED.getCode())
                        .msg(StatusEnum.SUCCEED.getMsg())
                        .operationTime(new Date())
                        .data((UserVo)obj)
                        .build();
            }else {
                return ResponseWrap.<UserVo>builder()
                        .code(StatusEnum.SUCCEED.getCode())
                        .msg(StatusEnum.SUCCEED.getMsg())
                        .operationTime(new Date())
                        .data(null)
                        .build();
            }
        } catch (Exception e) {
            log.error("当前用户：{}", ExceptionsUtil.getStackTraceAsString(e));
            throw new ProjectException(StatusEnum.ISLOGIN_FAIL.getCode(),
                    StatusEnum.ISLOGIN_FAIL.getMsg());
        }
    }
}
